package com.example.book.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 常量类
 *
 * @author haocj
 */
public class Constants {

    // 接口校验参数
    public static final String SIGNATURE = "signature";
    public static final String TIME_STAMP = "timestamp";

    public static final String GET_METHOD = "GET";
    public static final String POST_METHOD = "POST";

    public static final String IP_QUEUE = "IP_QUEUE";

    /**
     * 文件存储路径标记
     */
    public static final String FILEPATH = "/profile";

    private static final Logger logger = LoggerFactory.getLogger(Constants.class);

    private static Constants constants = new Constants();

    private static Map<String, String> map = new HashMap<>();

    public static final String  ONE_SPEECH = "one_speech_token";

    /**
     * 获取实力静态对象
     *
     * @return
     */
    public static Constants getInstance() {
        return constants;
    }

    /**
     * 删除标记
     */
    public static final String DEL_FLAG = "0";

    public static final String LOGIN_FLAG_0 = "0";
    public static final String LOGIN_FLAG = "1";


    /**
     * Ajax
     */
    public static final Integer SUCCESS = 0;
    public static final Integer ERROR = -1;

    /**
     * 禁止登录
     */
    public static final Integer RESTRICTED_LOGIN = -2;

    public static final String SUCCESS_MSG = "success";
    public static final String ERROR_MSG = "error";

    /**
     * 用户
     */
    public static final String CURRENT_USER = "loginUser";
    public static final String SUPER_ADMIN = "1";

    public static final String WX_API_TOKEN = "wx_api_token_";


    /**
     * shiro
     */
    public static final String CURRENT_ENABLED = "captchaEnabled";
    public static final String CURRENT_TYPE = "captchaType";
    public static final String CURRENT_VALIDATECODE = "validateCode";
    public static final String CURRENT_CAPTCHA = "captcha";
    public static final String CAPTCHA_ERROR = "captchaError";
    public static final String KAPTCHA_SESSION_KEY = "KAPTCHA_SESSION_KEY";
    public static final String SHIRO_KEY = "SHIRO_KEY";

    public static final String USER_CACHE = "USER_CACHE_";
    public static final String USER_ROLE_CACHE = "USER_ROLE_CACHE_";
    public static final String USER_PERMISSION_CACHE = "USER_PERMISSION_CACHE_";


    /**
     * jjwt
     */
    public static final String JWT_SECRET = "5feceb66ffc8667896c79c2dbc239dd4e91b46729d73a27fb57e9";

    /**
     * 代码生成状态
     */
    public static Integer GENERRATOR_STATUS_0 = -1;
    public static Integer GENERRATOR_STATUS_2 = 2;

    public static final String right = "对";
    public static final String wrong = "错";

    /**
     * 获取指定范围内的随机数
     * @param x
     * @param y
     * @return
     */
    public static Integer getRandom(int x,int y){
        Random random = new Random();
        int r= random.nextInt(x)+y;
        return r;
    }

    /**
     * 生成uuid
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    private static final PropertiesLoader loader = new PropertiesLoader("properties/system.properties");

    /**
     * 获取配置文件参数
     *
     * @param key
     * @return
     */
    public static String getKey(String key) {
        String value = map.get(key);
        if (StringUtils.isBlank(value)) {
            value = loader.getKey(key);
            map.put(key, value != null ? value : StringUtils.EMPTY);
        }
        return value;
    }

    /**
     * 获取工程路径
     * @return
     */
    public static String getProjectPath() {
        String path = null;
        try {
            File file = new DefaultResourceLoader().getResource("").getFile();
            if (file != null) {
                while (true) {
                    if (file == null || file.exists()) {
                        break;
                    }
                    if (file.getParentFile() != null) {
                        file = file.getParentFile();
                    } else {
                        break;
                    }
                }
                path = file.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return path;
    }
}
